﻿using System;

namespace exp2_3
{
    //定义用户类
    class User
    {
        private string _userName;

        public User(string name)
        {
            this._userName = name;
        }

        public void DispMethod()
        {
            Console.WriteLine("{0}已经收到订阅的推送",_userName);
        }

    }
    class Delegate
    {
        //定义委托
        public delegate void MeDelegate();
        //定义事件
        public event MeDelegate NotifyUsers;

        public void Notify()
        {
            
            if (NotifyUsers != null)
            {
                Console.WriteLine("触发事件：");
                NotifyUsers();
            }
        }


    }

    class Program
    {
        static void Main(string[] args)
        {
            /*目标
             1.使用事件和委托实现订阅推送功能。*/
            //定义委托对象
            Delegate objDelegate = new Delegate();
            //定义两个用户对象
            User user1 = new User("可乐");
            User user2 = new User("伍六七");
            //让两个用户订阅事件
            objDelegate.NotifyUsers += new Delegate.MeDelegate(user1.DispMethod);
            objDelegate.NotifyUsers += new Delegate.MeDelegate(user2.DispMethod);
            //开始推送
            objDelegate.Notify();
        }
    }
}